package com.motion.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.motion.generator.entity.UserSignInDay;
import com.motion.generator.mapper.UserSignInDayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class UserSignInDayService {
    @Autowired(required = false)
    private UserSignInDayMapper userSignInDayMapper;

    public Long insertUserSignInDay(int time,String CourseName,Long userId){
        UserSignInDay userSignInDay=new UserSignInDay();
        userSignInDay.setExerciseCourseNameList(CourseName);
        userSignInDay.setExercisetime(time);
        userSignInDay.setUserId(userId);
        userSignInDay.setExerciseDate(LocalDate.now());
        userSignInDayMapper.insert(userSignInDay);
        return userSignInDay.getUserId();
    }
    public Long updataUserSignInDay(int time,String courseName,Long userId){
        QueryWrapper<UserSignInDay> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.orderByDesc("exerciseDate");
        UserSignInDay tem = userSignInDayMapper.selectList(queryWrapper).get(0);
        Set<String> nameSet = new HashSet<>(Arrays.asList(tem.getExerciseCourseNameList().split(";")));
        nameSet.add(courseName);
        tem.setExerciseCourseNameList(String.join(";", nameSet));
        tem.setExercisetime(tem.getExercisetime()+time);
        userSignInDayMapper.updateById(tem);
        return tem.getUserId();
    }

    public List<UserSignInDay> getSignInDay(Long useId){
        QueryWrapper<UserSignInDay> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",useId);
        return userSignInDayMapper.selectList(queryWrapper);
    }
}
